//
//  KLMDeviceListCell.swift
//  KLM_GN
//
//  Created by Nic on 2025/2/27.
//

import UIKit

typealias OnOffBlock = (_ on: Bool) -> ()

class KLMDeviceListCell: UICollectionViewCell {

    @IBOutlet weak var iconView: UIButton!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var roomLabel: UILabel!
    @IBOutlet weak var modeLabel: UILabel!
    @IBOutlet weak var onOffView: UIButton!
    @IBOutlet weak var bgView: UIImageView!
    
    var onOffBlcok: OnOffBlock?
    
    var deviceInfo: KLMHomeDevice! {
        didSet {
            nameLabel.text = deviceInfo.deviceName
            roomLabel.text = deviceInfo.roomName
            modeLabel.text = deviceInfo.sceneName
            
            onOffView.isSelected = deviceInfo.ledEnable != 0
            let icon = KLMSmartNode.getIcon(deviceType: nodeDeviceType(rawValue: deviceInfo.deviceType) ?? .lineLamp)
            iconView.setImage(UIImage(named: icon), for: .normal)
        }
    }
    var scanDeviceInfo: DiscoveredPeripheral! {
        didSet {
            nameLabel.text = scanDeviceInfo.device.name
            let icon = KLMSmartNode.getIcon(deviceType: scanDeviceInfo.deviceType)
            iconView.setImage(UIImage(named: icon), for: .normal)
            roomLabel.text = "\(scanDeviceInfo.device.uuid)"
            modeLabel.text = scanDeviceInfo.deviceType.rawValue
//            onOffView.isSelected = deviceInfo.on
            onOffView.isEnabled = false
            onOffView.setImage(UIImage(named: getRssiIcon(level: scanDeviceInfo.Rssi)), for: .disabled)
        }
    }
    
    func getRssiIcon(level: RssiLevel) -> String {
        switch level {
        case .one:
            "Device_signal_1"
        case .two:
            "Device_signal_2"
        case .three:
            "Device_signal_3"
        case .four:
            "Device_signal_4"
        default:
            "Device_signal_0"
        }
    }
    func updateRssi(_ rssi: RssiLevel) {
        onOffView.setImage(UIImage(named: getRssiIcon(level: rssi)), for: .disabled)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        layer.shadowColor = rgba(0, 0, 0, 0.4).cgColor
        layer.shadowOffset = CGSize(width: 0, height: 2)
        layer.shadowOpacity = 0.4
        layer.shadowRadius = 8
        layer.masksToBounds = false
//        backgroundColor = .white
        
        iconView.isEnabled = false
        iconView.imageView?.contentMode = .scaleAspectFit
        iconView.backgroundColor = .white
        iconView.layer.cornerRadius = 8.0
        iconView.layer.borderColor = rgba(217, 217, 217, 1).cgColor
        iconView.layer.borderWidth = 1
        iconView.layer.masksToBounds = true
        
        iconView.setImage(UIImage(named: "Device_type_lamp"), for: .normal)
        
        iconView.configStyle(bgImage: UIImage.image(withColor: .white, size: iconView.size, borderColor: rgba(246, 246, 246, 1), borderWidth: 2))
        
        nameLabel.configStyle(font: UIFont(safeName: "PingFangSC-Regular", size: 16), titleColor: rgba(0, 0, 0, 1))
        roomLabel.configStyle(font: UIFont(safeName: "PingFangSC-Regular", size: 12), titleColor: rgba(138, 138, 138, 1))
        modeLabel.configStyle(font: UIFont(safeName: "PingFangSC-Regular", size: 12), titleColor: rgba(138, 138, 138, 1))
        
        onOffView.configStyle(image: "switch_off")
        onOffView.configStyle(image: "switch_on", state: .selected)
    }

    @IBAction func tapIcon(_ sender: UIButton) {
        
    }
    @IBAction func tapOnOff(_ sender: UIButton) {
//        sender.isSelected = !sender.isSelected
        if let onOffBlcok = onOffBlcok {
            onOffBlcok(!sender.isSelected)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        bgView.image = UIImage.image(withColor: .white, size: bounds.size, cornerRadius: 8.0, borderColor: rgba(243, 243, 246, 1), borderWidth: 1)
    }
}
